# function DifferentiateArray(y,x) # Returns array dy/dx[1…n] at x[1…n].
# function IntegrateArray(y,x) # Returns array of indefinite integral of y[1…n].
# function Integrate(f,a,b) # Returns definite integral of f(x) dx from x=a to b.
#
# Also see the routines in the Miscellaneous Routines file, which do some
# of the same things without using xCOD's.
#
# This text file explains and gives examples of
# some of the routines in the file All Library Routines, which
# should be Opened before trying any of these examples.
#################
###########
# These are the xCODs, which are in the file
#
xDIFFERENTIATE_ARRAY
# xCOD xDIFFERENTIATE_ARRAY(N:num; X,Y, dy2, yDiff:RealArray), finds the derivative yDiff[1…N] of y[1…n] at x[1…n]. dy2[1…n] returns with the 2nd derivatives of y(x), used in the cubic spline fit; an external program.
xINTEGRATE_ARRAY
# xCOD xINTEGRATE_ARRAY(N:num; X,Y, dy2,Yintegral:RealArray), finds the indefinite integral yIntegral[1…N] of y[1…n] at x[1…n]. dy2[1…n] returns with the 2nd derivatives of y(x), used in the cubic spline fit; an external program.
xINTEGRATE
# xCOD xINTEGRATE(Prog SumF(n:num; zIO:array); a,b,Tol,Ans,Err:Num), finds Ans= definite integral of f(x) from a to b within Tol. SumF(4,zIO=[S,x1,x2,dx]) must calculate S=f(x1)+f(x1+dx)+…+f(x2)). Err: -1=> did not converge, lower Tol; an external program
#############
# Here are the interface routines.
# Given arrays x[1…n], y[1…n] with y=f(x),
# this returns an array of the first derivatives of y at x.
# xDIFFERENTIATE_ARRAY, like xINTEGRATE_ARRAY,
# uses a cubic spline fit to y(x), which calculates the
# 2nd derivatives dy2[1…n] of y(x).
#
function DifferentiateArray(y,x) # Returns array dy/dx[1…n] at x[1…n].
. var n, dy2, ans
. # Input:
. # x, y = arrays
. # Output:
. # DifferentiateArray = dy/dx array
. begin
. dy2=y; ans=y # reserve storage space.
. n = size(y)
. xDIFFERENTIATE_ARRAY(n,x,y,dy2,ans)
. DifferentiateArray = ans
. end
.
# This is the inverse of DifferentiateArray.
# Given x[1…n], y[1…n] with y=f(x),
# IntegrateArray returns an array for the indefinite
# integral of y from x[1] to x[j] at the points x[j].
# The first value of the returned result is
# always zero, since it is the integral from x[1] to x[1].
#
function IntegrateArray(y,x) # Returns array of indefinite integral of y[1…n].
. var n,dy2,ans
. # Input:
. # x,y = arrays
. # Output:
. # IntegrateArray = Integral from x[1] to x of y, an array
. #
. begin
. dy2=y; ans=y # reserve storage space.
. n = size(y)
. xINTEGRATE_ARRAY(n,x,y,dy2,ans)
. IntegrateArray = ans
. end
.
# Integrate returns the definite integral of
# an analytic function f(x) from x=a to x=b.
# For this interface routine,
# f(x) should be an NCII function defined via a line like
# f(x) = x^2+3
# or whatever.
# (You can pass another xCOD for SumF, as
# long as it takes the two arguments zN,zIO=[S,x0,x1,dx]
# and calculates S=f(x0)+f(x0+dx)+…+f(x1). )
#
# Set "Tol" to the desired accuracy of the result.
#
function Integrate(f,a,b) # Returns definite integral of f(x) dx from x=a to b.
. var tol, ans, err
. # Input:
. # NCII user function f
. # a, b = numbers for left, right of integration range
. # Output:
. # Integrate = integral of f from a to b, a number.
. begin # first, rename the function f(x).
. zzF_Integral__ = f
. tol = 1e-8 # going too high will demand very large arrays in SumF
. xINTEGRATE(zzFuncForIntegrate__, a, b, tol, ans, err)
. if err<>0 then
. begin
. beep;
. Print(" Integrate did not converge ");
. end
. Integrate = ans
. end
.
# This is used by Integrate to evaluate
# the function that is to be integrated.
# The xCOD xINTEGRATE calls this
# several times with different grid sizes,
# converging on the final value of the integral.
# zIO[1…4] is both input and output.
# Input:
# zIO[2] = x1
# zIO[3] = x2
# zIO[3] = dx
# Output:
# zIO[1] = sum( f(x1)+f(x1+dx)+...+f(x2) )
# where f is the function zzF_Integral__ ,
# which is set from the passed function in Integrate, above.
#
program zzFuncForIntegrate__(n4, zIO) # used with function Integrate(f,a,b)
. var x
. begin
. x = zIO(2)…zIO(3)@zIO(4)
. zIO(1) = sum(zzF_Integral__(x))
. end
.
###########
# Examples: #
###########
####
# Differentiation and Integration of arrays.
#
# First, define the independent array x:
x = 0…π@.02;
# Then the function y, and the exact derivative and integral.
y= sin(x);
ExactDeriv=cos(x);
ExactIntegral = 1-cos(x); # The 1 is the integration constant,
# chosen so that ExactIntegral[1]=0.
# Then the approximate derivative and integral, using the
# xCOD routines which fit cubic splines to the arrays :
approxDeriv = DifferentiateArray(y,x);
approxIntegral = IntegrateArray(y,x);
#
# And a table of some of the results. To generate the full table,
# evaluate the "Table" line below, without the "#" at the front. Most of the